//Start functions of Property
var PropertyTemplate = "templates/Property.xsl"
var getTypesTemplate = "templates/getTypes.xsl"
var getPropertyTemplate = "templates/getProperty.xsl"

function loadPropertyData(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.property?id=' + xID + '&m_id=' + modelID , true)
		objectHTTPXML.onreadystatechange = returnPropertyData
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnPropertyData(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(PropertyTemplate, xml)
		document.getElementById("divBody").innerHTML = template
		waitBox.hideBox(null);
		insertType();
	}			
}

function saveProperty(xID){
	if(objectHTTPXML) {
		var parameters = "m_id=" + modelID +
						 "&id=" + encodeURI(document.getElementById("id").value) +
						 "&name=" + encodeURI(document.getElementById("name").value) +
						 "&lower=" + encodeURI(document.getElementById("lower").value) +
						 "&upper=" + encodeURI(document.getElementById("upper").value) +
						 "&default=" + encodeURI(document.getElementById("default").value)
		
		if(document.getElementById("isOrdered").checked){
			parameters += "&isOrdered=" + 1
		}
		if(document.getElementById("isUnique").checked){
			parameters += "&isUnique=" + 1
		}
		if(document.getElementById("isReadOnly").checked){
			parameters += "&isReadOnly=" + 1
		}
		if(document.getElementById("isComposite").checked){
			parameters += "&isComposite=" + 1
		}
		if(document.getElementById("isDerived").checked){
			parameters += "&isDerived=" + 1
		}
		if(document.getElementById("isID").checked){
			parameters += "&isID=" + 1
		}
		
		if(document.getElementById("type").value != ""){
			parameters += "&type=" + document.getElementById("type").value
		}
		
		if(document.getElementById("opposite").value != ""){
			parameters += "&opposite=" + document.getElementById("opposite").value
		}

		xIDTmp = xID

		waitBox = new Lightbox.base('box1');

		objectHTTPXML.onreadystatechange = returnSaveProperty
		objectHTTPXML.open('POST', '/model.save.property', true);
		objectHTTPXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objectHTTPXML.setRequestHeader("Content-length", parameters.length);
		objectHTTPXML.setRequestHeader("Connection", "close");
		objectHTTPXML.send(parameters);
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnSaveProperty(){
	if (objectHTTPXML.readyState == 4) {
		waitBox.hideBox(null);

		if(objectHTTPXML.responseText == 'OK'){
			changeName(xIDTmp, document.getElementById("name").value)
			alert('Data saved!')
		} else {
			alert('Data not saved!')
			loadPropertyData(xIDTmp)
		}
	}			
}

function createProperty(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.create.property?&m_id=' + modelID + '&p_id=' + xID , true)
		objectHTTPXML.onreadystatechange = returnProperty
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnProperty(){
	if (objectHTTPXML.readyState == 4) {
		if(objectHTTPXML.responseText.split("$$$$").length != 0){
			id = objectHTTPXML.responseText.split("$$$$")
			createNewNode(xIDTmp, id[0], id[1], 'javascript:loadPropertyData("' + id[0] + '");', 'property.gif', 'property.gif')
		} else {
			alert('Error to create the Property!')
		}
		waitBox.hideBox(null);
	}			
}

function insertType(){
	if(objectHTTPXML) {
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.alltypes?&m_id=' + modelID, true)
		objectHTTPXML.onreadystatechange = returnInsertType
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnInsertType(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(getTypesTemplate, xml)
		document.getElementById("divType").innerHTML = template

		var typeId = document.getElementById("typeId").value
		var type = document.getElementById("type")
		
		for(i = 0; i < type.options.length; ++i){
			if(type.options[i].value == typeId){
				type.selectedIndex = i
				break
			}
		}
		
		waitBox.hideBox(null);
		insertOpposite(document.getElementById("class").value)
	}			
}

function insertOpposite(id){
	if(objectHTTPXML) {
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.allproperties?&m_id=' + modelID + '&id=' + id, true)
		objectHTTPXML.onreadystatechange = returnInsertOpposite
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnInsertOpposite(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(getPropertyTemplate, xml)
		document.getElementById("divOpposite").innerHTML = template
		
		var oppositeId = document.getElementById("oppositeId").value
		var opposite = document.getElementById("opposite")
		var id = document.getElementById("id").value
		
		for(i = 0; i < opposite.options.length; ++i){
			if(opposite.options[i].value == id){
				opposite.remove(i)
				break
			}
		}		

		for(i = 0; i < opposite.options.length; ++i){
			if(opposite.options[i].value == oppositeId){
				opposite.selectedIndex = i
				break
			}
		}		
		waitBox.hideBox(null);
	}			
}

//End functions of Property